using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using JGSY.CMS.LowCode.Platform.Domain.Interfaces;

namespace JGSY.CMS.LowCode.Platform.Domain.Entities
{
    /// <summary>
    /// 表单定义实体
    /// 用于创建和管理动态表单，支持JSON字段配置
    /// </summary>
    [Table("forms")]
    public class Form : ITenantEntity
    {
        /// <summary>
        /// 租户ID
        /// </summary>
        [Required]
        [StringLength(36)]
        [Column("tenant_id")]
        public string TenantId { get; set; } = string.Empty;

        /// <summary>
        /// 主键ID
        /// </summary>
        [Key]
        public long Id { get; set; }

        /// <summary>
        /// 表单名称
        /// </summary>
        [Required]
        [StringLength(255)]
        public string Name { get; set; } = string.Empty;

        /// <summary>
        /// 表单描述
        /// </summary>
        [Column(TypeName = "text")]
        public string? Description { get; set; }

        /// <summary>
        /// 表单字段配置
        /// JSON格式存储字段定义和属性
        /// </summary>
        [Column(TypeName = "json")]
        public string? Fields { get; set; }

        /// <summary>
        /// 表单样式配置
        /// JSON格式存储表单样式设置
        /// </summary>
        [Column(TypeName = "json")]
        public string? Styles { get; set; }

        /// <summary>
        /// 验证规则配置
        /// JSON格式存储字段验证规则
        /// </summary>
        [Column(TypeName = "json")]
        public string? Rules { get; set; }

        /// <summary>
        /// 通知配置
        /// JSON格式存储表单提交后的通知设置
        /// </summary>
        [Column(TypeName = "json")]
        public string? NotifyConfig { get; set; }

        /// <summary>
        /// 表单设置
        /// JSON格式存储其他表单设置（如提交限制、验证码等）
        /// </summary>
        [Column(TypeName = "json")]
        public string? Settings { get; set; }

        /// <summary>
        /// 提交成功消息
        /// </summary>
        [StringLength(500)]
        public string? SuccessMessage { get; set; }

        /// <summary>
        /// 提交成功页面URL
        /// </summary>
        [StringLength(255)]
        public string? SuccessUrl { get; set; }

        /// <summary>
        /// 表单模板代码
        /// 自定义HTML模板
        /// </summary>
        [Column(TypeName = "text")]
        public string? Template { get; set; }

        /// <summary>
        /// 是否需要登录
        /// </summary>
        public bool RequireLogin { get; set; } = false;

        /// <summary>
        /// 是否需要验证码
        /// </summary>
        public bool RequireCaptcha { get; set; } = false;

        /// <summary>
        /// 允许匿名提交
        /// </summary>
        public bool AllowAnonymous { get; set; } = true;

        /// <summary>
        /// 是否允许多次提交
        /// </summary>
        public bool AllowMultipleSubmissions { get; set; } = true;

        /// <summary>
        /// 提交次数统计
        /// </summary>
        public int SubmissionCount { get; set; } = 0;

        /// <summary>
        /// 浏览次数
        /// </summary>
        public int ViewCount { get; set; } = 0;

        /// <summary>
        /// 排序权重
        /// </summary>
        public int SortOrder { get; set; } = 0;

        /// <summary>
        /// 表单版本
        /// </summary>
        public int Version { get; set; } = 1;

        /// <summary>
        /// 状态（启用/禁用）
        /// 0=禁用, 1=启用, 2=草稿
        /// </summary>
        public byte Status { get; set; } = 2;

        /// <summary>
        /// 创建人
        /// </summary>
        [Required]
        public long CreatedBy { get; set; }

        /// <summary>
        /// 修改人
        /// </summary>
        public long? UpdatedBy { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreatedAt { get; set; } = DateTime.UtcNow;

        /// <summary>
        /// 修改时间
        /// </summary>
        public DateTime UpdatedAt { get; set; } = DateTime.UtcNow;

        /// <summary>
        /// 创建人导航属性
        /// </summary>
        [ForeignKey(nameof(CreatedBy))]
        public User? Creator { get; set; }

        /// <summary>
        /// 修改人导航属性
        /// </summary>
        [ForeignKey(nameof(UpdatedBy))]
        public User? Updater { get; set; }

        /// <summary>
        /// 租户导航属性
        /// </summary>
        public Tenant? Tenant { get; set; }

        /// <summary>
        /// 该表单的提交数据集合
        /// </summary>
        [InverseProperty(nameof(FormSubmission.Form))]
        public ICollection<FormSubmission> Submissions { get; set; } = new List<FormSubmission>();
    }
}
